package Hxw3DEngine.proxy
{
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import Hxw3DEngine.core.controller.Camera3D;
	import Hxw3DEngine.error.ErrorType;
	
	import flash.display.Stage;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProfile;
	import flash.utils.ByteArray;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Context3DCompareMode;
	

	/**
	 *计录stage3d的状态以及将常用的数据存储于全局方便备用 
	 * @author huangxw
	 */	
	public class Stage3DProxy
	{
		private static var _insdance:Stage3DProxy;
		
		public function Stage3DProxy():void{
			if(_insdance){
				throw new Error(ErrorType.SINGLE_ERROR);
			}
		}
		
		public  static function get insdance():Stage3DProxy{
			return _insdance ||= new Stage3DProxy
		}
		
		
		//--------------------------------------------------------------------------------------------
		
		
		//context3d
		private var _context3d:Context3D;
		
		public function get context3d():Context3D{
			return _context3d;
		}
		
		public function set context3d(value:Context3D):void{
			_context3d = value;
		}
		
		public static function get context3d():Context3D{
			return insdance.context3d;
		}
		
		
		public static function clear(color:uint = 0x0000ff):void{
			context3d.clear(0,0,1);
			clearBuffer();
		}
		
		public static function present():void{
			context3d.present()
		}
		
		
		public static function clearBuffer():void{
			for (var i:int = 0;i<8;i++){
				context3d.setVertexBufferAt(i,null);	
				context3d.setTextureAt(i,null);
			}
		}
		
		//stage
		private var _stage:Stage;
		
		public function get stage():Stage{
			return _stage;
		}
		
		public function set stage(value:Stage):void{
			_stage = value;
		}
		
		public static function get stage():Stage{
			return insdance.stage;
		}
		
		
		public static function get stageWidth():Number{
			return stage.stageWidth;
		}
		
		public static function get stageHeight():Number{
			return stage.stageHeight;
		}
		
		//camera
		private var _camera:Camera3D;
		
		public function get camera():Camera3D{
			return _camera;
		}
		
		public function set camera(value:Camera3D):void{
			_camera = value;
		}
		
		public static function get camera():Camera3D{
			return insdance.camera;
		}
		
		//渲染需要用的全局变量在drawRenderable的时候赋值
		
		//是否开启debug模式
		public static var debug					:Boolean = true;
		//渲染gameobject数量
		public static var renderGameobjectCount			:int = 0;
		//渲染总面数
		public static var renderTriangles		:int = 0;
		/** gpu模式 */
		public static var profile 				: String = Context3DProfile.BASELINE;
		/** drawcall次数 */		
		public static var drawCalls  			: int = 0;
		/** 最大矩阵骨骼数 */
		public static const MAX_MATRIX34_BONE   : int = 36;
		/** 最大四元数骨骼数 */
		public static const MAX_QUAT_BONE		: int = 56;
		/** 骨骼数据 */
		public static var 	BoneMatrixs			: ByteArray;
		/** 默认灯光 */
		//public static const defaultDirectLight  : DirectionalLight = new DirectionalLight();
		/** 投影 */
		public static const proj				: Matrix3D = new Matrix3D();
		/** view */
		public static const view				: Matrix3D = new Matrix3D();
		/** mvp */
		public static const mvp					: Matrix3D = new Matrix3D();
		/** world */
		public static const world				: Matrix3D = new Matrix3D();
		/** 相机坐标 */
		public static const cameraPos			: Vector3D = new Vector3D();
		/** 相机方向 */
		public static const cameraDir			: Vector3D = new Vector3D();
		
		/** view projection */		
		public static var viewProjection 		: Matrix3D = new Matrix3D();
		/** 默认混合模式 */
		public static var defaultSourceFactor	: String = Context3DBlendFactor.ONE;
		/** 默认混合模式 */
		public static var defaultDestFactor 	: String = Context3DBlendFactor.ZERO;
		/** 默认裁减 */
		public static var defaultCullFace 		: String = Context3DTriangleFace.BACK;
		/** 默认深度测试 */
		public static var defaultDepthWrite		: Boolean = true;
		/** 默认深度测试条件 */
		public static var defaultCompare		: String = Context3DCompareMode.LESS_EQUAL;
		
		/** 最大贴图尺寸 */
		public static const MAX_TEXTURE_SIZE    : int = 2048;
		
		
	}
	
}